package utils.ai
{
	import global.GameType;
	
	import utils.rule.IRuleResult;
	import utils.rule.RuleResult;
	//手动出牌
	public class ReplyPlayCard
	{
		//出3张牌
		public static function replay3PlayCard(lastCardDatas:Array,playCardDatas:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (lastCardDatas.length < 3)
			{
				return ruleResult;
			}
			
			playCardDatas = CardTools.cloneCardDatas(playCardDatas);
			
			var jokers:Array = CardTools.getJokers(playCardDatas);
			var rest:Array = CardTools.getRest(playCardDatas);
			var results:Array = [];
			ruleResult.results = results;
			
			if (playCardDatas.length == 2 && jokers.length == 2)
			{
				ruleResult.correct = true;
				results.push(playCardDatas);
			}
			else
			{
				if (CardTools.isCardDatasTheSame(lastCardDatas))//如果上次出牌是炸弹
				{
					switch (jokers.length)
					{
						case 0:
							if (CardTools.isCardDatasTheSame(playCardDatas))//炸弹
							{
								if (CardTools.compairTwoBombs(playCardDatas,lastCardDatas))
								{
									ruleResult.correct = true;
									results.push(playCardDatas);
								}
							}
							break;
						case 1:
							if (rest.length <= 3)
							{
								var result:Array = CardTools.setJokerAsOthers(jokers[0],rest);
								if (CardTools.isCardDatasTheSame(result))
								{
									if (CardTools.compairTwoBombs(result,lastCardDatas))
									{
										ruleResult.correct = true;
										results.push(result);
									}
									
								}
							}
							break;
							
					}
				}
				else if (CardTools.isCardDatasStraight(lastCardDatas))//如果是连牌
				{
					switch (jokers.length)
					{
						case 0:
							if (CardTools.isCardDatasStraight(playCardDatas))
							{
								if (playCardDatas[0].value - 1 == lastCardDatas[0].value)
								{
									ruleResult.correct = true;
									results.push(playCardDatas);
								}
							}
							else if (CardTools.isCardDatasTheSame(playCardDatas))
							{
								ruleResult.correct = true;
								results.push(playCardDatas);
							}
							break;
						case 1:
							var result1:Array = CardTools.combineJokerWithRestToStraight(jokers[0],rest);
							if (result1.length > 0)
							{
								var array:Array;
								for (var i:String in result1)
								{
									array = result1[i];
									if (array[0].value - 1 == lastCardDatas[0].value)
									{
										ruleResult.correct = true;
										results.push(array);
										break;
									}
								}
							}
							else if (rest.length <= 3) 
							{
								var result2:Array = CardTools.setJokerAsOthers(jokers[0],rest);
								if (CardTools.isCardDatasTheSame(result2))
								{
									ruleResult.correct = true;
									results.push(result2);
								}
							}
							break;
					}
				}
				else
				{
					var lastpairStarightResult:IRuleResult = CardTools.isPairStraight(lastCardDatas);
					if (lastpairStarightResult.correct)//如果是板凳对
					{
						
						
						switch (jokers.length)
						{
							case 0:
								if (CardTools.isPairStraight(playCardDatas).correct)
								{
									if (playCardDatas[0].value - 1 == lastCardDatas[0].value && playCardDatas.length == lastCardDatas.length)
									{
										ruleResult.correct = true;
										results.push(playCardDatas);
									}
								}
								else if (CardTools.isCardDatasTheSame(playCardDatas))//炸弹
								{
									ruleResult.correct = true;
									results.push(playCardDatas);
								}
								break;
							case 1:
								var setPairResult:Array = CardTools.setJokerPairStarightGap(jokers[0],rest);
								if (setPairResult.length> 0)
								{
									for (var n:String in setPairResult)
									{
										if (setPairResult[n][0].value - 1 == lastCardDatas[0].value && setPairResult[n].length == lastCardDatas.length)
										{
											ruleResult.correct = true;
											results.push(setPairResult[n]);
											break;
										}
									}	
								}
								else if (rest.length <= 3)
								{ 
									var setResult:Array = CardTools.setJokerAsOthers(jokers[0],rest);
									if(CardTools.isCardDatasTheSame(setResult))//炸弹
									{
										ruleResult.correct = true;
										results.push(setResult);
									}
								}
								break;
						}
					}
				}
			}
			
			return ruleResult;
		}
		
		//出2张牌
		public static function replay2PlayCard(lastCardDatas:Array,playCardDatas:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (lastCardDatas.length != 2)
			{
				return ruleResult;
			}
			
			playCardDatas = CardTools.cloneCardDatas(playCardDatas);
			
			var jokers:Array = CardTools.getJokers(playCardDatas);
			var rest:Array = CardTools.getRest(playCardDatas);
			var results:Array = [];
			ruleResult.results = results;
			
			if (lastCardDatas[0].value == GameType.CARD_VALUE_JOKER && CardTools.isCardDatasTheSame(lastCardDatas))
			{
				return ruleResult;
			}
			
			if (playCardDatas.length == 2)
			{
				switch (jokers.length)
				{
					case 0:
						if ( CardTools.compairTwoCards(playCardDatas[0],lastCardDatas[0]))
						{
							ruleResult.correct = true;
							results.push(playCardDatas);
						}	
						break;
					case 1:
						var result:Array = CardTools.setJokerAsOthers(jokers[0],rest);
						if ( CardTools.compairTwoCards(result[0],lastCardDatas[0]))
						{
							ruleResult.correct = true;
							results.push(result);
						}	
						break;
					case 2:
						ruleResult.correct = true;
						results.push(playCardDatas);
						break;
				}
			}
			else if (playCardDatas.length > 2)
			{
				switch (jokers.length)
				{
					case 0:
						if (CardTools.isCardDatasTheSame(playCardDatas))
						{
							if (rest.length >= 3)
							{
								ruleResult.correct = true;
								results.push(playCardDatas);
							}
						}
						break;
					case 1:
						var results1:Array = CardTools.setJokerAsOthers(jokers[0],rest);
						if (CardTools.isCardDatasTheSame(results1))
						{
							if (rest.length <= 3)
							{
								ruleResult.correct = true;
								results.push(results1);
							}
						}
						break;
				}
			}
			
			return ruleResult;
		}
		
		//出1张牌
		public static function replay1PlayCard(lastCardDatas:Array,playCardDatas:Array):IRuleResult
		{
			var ruleResult:IRuleResult = new RuleResult();
			if (lastCardDatas.length != 1)
			{
				return ruleResult;
			}
			
			playCardDatas = CardTools.cloneCardDatas(playCardDatas);
			
			var jokers:Array = CardTools.getJokers(playCardDatas);
			var results:Array = [];
			ruleResult.results = results;
			if (playCardDatas.length == 1)
			{ 
				ruleResult.correct = CardTools.compairTwoCards(playCardDatas[0],lastCardDatas[0]);
				results.push(playCardDatas);
			}
			else if (playCardDatas.length == 2)
			{
				if (jokers.length == 2)
				{
					ruleResult.correct = true;
					results.push(playCardDatas);
				}
			}
			else if (playCardDatas.length > 2)
			{
				if (jokers.length == 0)
				{
					if (CardTools.isCardDatasTheSame(playCardDatas))
					{
						ruleResult.correct = true;
						results.push(playCardDatas);
					}
				}
				else if (jokers.length == 1)
				{
					var rest:Array = CardTools.getRest(playCardDatas);
					var result:Array = CardTools.setJokerAsOthers(jokers[0],rest);
					if (CardTools.isCardDatasTheSame(result))//如果是一样的
					{
						if (rest.length <= 3)
						{
							ruleResult.correct = true;
							results.push(result);
						}
					}
				}
			}
			
			return ruleResult;
		}
	}
}